<template>
  <div class="product-card" :class="{ 'product-card-hover': hoverable }">
    <!-- 商品图片 -->
    <div class="product-image">
      <router-link :to="`/product/detail?id=${product.id}`" class="image-link">
        <img :src="product.image || '/default-product.png'" :alt="product.name" class="product-img">
      </router-link>
      
      <!-- 商品标签 -->
      <div v-if="product.tags && product.tags.length > 0" class="product-tags">
        <span v-for="tag in product.tags" :key="tag" class="product-tag">{{ tag }}</span>
      </div>
      
      <!-- 商品操作按钮 -->
      <div class="product-actions" v-if="showActions">
        <button 
          v-if="!isFavorite" 
          @click="handleFavorite" 
          class="action-btn favorite-btn"
          title="收藏"
        >
          <el-icon><Star /></el-icon>
        </button>
        <button 
          v-else 
          @click="handleFavorite" 
          class="action-btn favorite-btn active"
          title="取消收藏"
        >
          <el-icon><StarFilled /></el-icon>
        </button>
        <button 
          @click="handleAddToCart" 
          class="action-btn add-cart-btn"
          title="加入购物车"
        >
          <el-icon><ShoppingCart /></el-icon>
        </button>
        <button 
          @click="handleBuyNow" 
          class="action-btn buy-now-btn"
          title="立即购买"
        >
          <el-icon><Ticket /></el-icon>
        </button>
      </div>
    </div>
    
    <!-- 商品信息 -->
    <div class="product-info">
      <!-- 商品分类 -->
      <div v-if="product.categoryName" class="product-category">
        {{ product.categoryName }}
      </div>
      
      <!-- 商品名称 -->
      <router-link :to="`/product/detail?id=${product.id}`" class="product-name-link">
        <h3 class="product-name">{{ product.name }}</h3>
      </router-link>
      
      <!-- 商品描述 -->
      <p v-if="product.description" class="product-description">{{ product.description }}</p>
      
      <!-- 商品评分 -->
      <div v-if="product.rating" class="product-rating">
        <el-rate 
          :model-value="product.rating" 
          disabled 
          :show-score="showRatingScore" 
          score-template="{{ value }}"
          size="small"
        />
        <span v-if="product.reviewCount" class="review-count">({{ product.reviewCount }})</span>
      </div>
      
      <!-- 商品价格 -->
      <div class="product-price">
        <span class="price-symbol">¥</span>
        <span class="price-value">{{ formatPrice(product.price) }}</span>
        <span v-if="product.originalPrice && product.originalPrice > product.price" class="original-price">
          ¥{{ formatPrice(product.originalPrice) }}
        </span>
      </div>
      
      <!-- 商品销量 -->
      <div v-if="product.salesVolume" class="product-sales">
        销量 {{ product.salesVolume }}
      </div>
    </div>
    
    <!-- 商品规格 -->
    <div v-if="product.specs && product.specs.length > 0" class="product-specs">
      <div v-for="spec in product.specs" :key="spec.name" class="spec-item">
        <span class="spec-name">{{ spec.name }}:</span>
        <span class="spec-value">{{ spec.value }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useCartStore } from '../../store/modules/cart'
import { Star, StarFilled, ShoppingCart, Ticket } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { formatPrice } from '../../utils/helper'

const props = defineProps({
  // 商品数据
  product: {
    type: Object,
    required: true,
    default: () => ({})
  },
  // 是否显示操作按钮
  showActions: {
    type: Boolean,
    default: true
  },
  // 是否显示评分分数
  showRatingScore: {
    type: Boolean,
    default: true
  },
  // 是否可收藏
  canFavorite: {
    type: Boolean,
    default: true
  },
  // 是否可加入购物车
  canAddToCart: {
    type: Boolean,
    default: true
  },
  // 是否可立即购买
  canBuyNow: {
    type: Boolean,
    default: true
  },
  // 卡片是否有悬停效果
  hoverable: {
    type: Boolean,
    default: true
  }
})

const router = useRouter()
const cartStore = useCartStore()
const isFavorite = ref(false)

// 处理收藏
const handleFavorite = () => {
  if (!props.canFavorite) return
  
  isFavorite.value = !isFavorite.value
  ElMessage.success(isFavorite.value ? '收藏成功' : '取消收藏')
  // 实际项目中这里应该调用收藏API
}

// 处理加入购物车
const handleAddToCart = () => {
  if (!props.canAddToCart) return
  
  const cartItem = {
    id: props.product.id,
    name: props.product.name,
    image: props.product.image,
    price: props.product.price,
    quantity: 1,
    skuId: props.product.skuId || 'default',
    specs: props.product.specs || []
  }
  
  cartStore.addToCart(cartItem)
  ElMessage.success('已加入购物车')
}

// 处理立即购买
const handleBuyNow = () => {
  if (!props.canBuyNow) return
  
  // 将商品添加到购物车并直接进入结算页面
  const cartItem = {
    id: props.product.id,
    name: props.product.name,
    image: props.product.image,
    price: props.product.price,
    quantity: 1,
    skuId: props.product.skuId || 'default',
    specs: props.product.specs || []
  }
  
  // 清空购物车并添加当前商品
  cartStore.clearCart()
  cartStore.addToCart(cartItem)
  
  // 跳转到结算页面
  router.push('/cart/checkout')
}
</script>

<style scoped>
.product-card {
  background-color: #fff;
  border: 1px solid $border-color;
  border-radius: $border-radius-base;
  padding: 16px;
  transition: all 0.3s;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.product-card-hover:hover {
  transform: translateY(-4px);
  box-shadow: $shadow-hover;
  border-color: $primary-color;
}

/* 商品图片 */
.product-image {
  position: relative;
  margin-bottom: 12px;
  overflow: hidden;
  border-radius: $border-radius-base;
}

.image-link {
  display: block;
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.product-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.product-card-hover:hover .product-img {
  transform: scale(1.05);
}

/* 商品标签 */
.product-tags {
  position: absolute;
  top: 8px;
  left: 8px;
  display: flex;
  gap: 4px;
}

.product-tag {
  padding: 2px 8px;
  background-color: $primary-color;
  color: #fff;
  font-size: 12px;
  border-radius: $border-radius-small;
}

/* 商品操作按钮 */
.product-actions {
  position: absolute;
  bottom: 8px;
  right: 8px;
  display: flex;
  gap: 8px;
  opacity: 0;
  transition: opacity 0.3s;
}

.product-card-hover:hover .product-actions {
  opacity: 1;
}

.action-btn {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background-color: #fff;
  color: $text-color;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: $shadow-base;
}

.action-btn:hover {
  background-color: $primary-color;
  color: #fff;
}

.favorite-btn.active {
  background-color: $primary-color;
  color: #fff;
}

.add-cart-btn {
  background-color: $success-color;
  color: #fff;
}

.add-cart-btn:hover {
  background-color: $success-color-dark;
}

.buy-now-btn {
  background-color: $danger-color;
  color: #fff;
}

.buy-now-btn:hover {
  background-color: $danger-color-dark;
}

/* 商品信息 */
.product-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* 商品分类 */
.product-category {
  font-size: $font-size-xs;
  color: $text-color-placeholder;
}

/* 商品名称 */
.product-name-link {
  text-decoration: none;
}

.product-name {
  font-size: $font-size-base;
  color: $text-color;
  font-weight: 500;
  line-height: 1.4;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.product-name:hover {
  color: $primary-color;
}

/* 商品描述 */
.product-description {
  font-size: $font-size-sm;
  color: $text-color-secondary;
  line-height: 1.4;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

/* 商品评分 */
.product-rating {
  display: flex;
  align-items: center;
  gap: 8px;
}

.review-count {
  font-size: $font-size-xs;
  color: $text-color-placeholder;
}

/* 商品价格 */
.product-price {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-top: 4px;
}

.price-symbol {
  font-size: $font-size-base;
  color: $danger-color;
  font-weight: bold;
}

.price-value {
  font-size: $font-size-lg;
  color: $danger-color;
  font-weight: bold;
}

.original-price {
  font-size: $font-size-sm;
  color: $text-color-placeholder;
  text-decoration: line-through;
}

/* 商品销量 */
.product-sales {
  font-size: $font-size-xs;
  color: $text-color-placeholder;
}

/* 商品规格 */
.product-specs {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid $border-color;
}

.spec-item {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 4px;
}

.spec-name {
  font-size: $font-size-xs;
  color: $text-color-placeholder;
}

.spec-value {
  font-size: $font-size-xs;
  color: $text-color-secondary;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .product-card {
    padding: 12px;
  }
  
  .image-link {
    height: 150px;
  }
  
  .product-actions {
    opacity: 1;
  }
  
  .action-btn {
    width: 32px;
    height: 32px;
    font-size: 14px;
  }
  
  .product-name {
    font-size: $font-size-sm;
  }
  
  .price-value {
    font-size: $font-size-base;
  }
}

@media (max-width: 480px) {
  .product-card {
    padding: 8px;
  }
  
  .image-link {
    height: 120px;
  }
}

/* 不同样式变体 */
.product-card.compact {
  padding: 12px;
}

.product-card.compact .image-link {
  height: 150px;
}

.product-card.featured {
  border: 2px solid $primary-color;
}

.product-card.discount {
  position: relative;
}

.product-card.discount::before {
  content: '限时折扣';
  position: absolute;
  top: -1px;
  right: -1px;
  padding: 2px 8px;
  background-color: $danger-color;
  color: #fff;
  font-size: 12px;
  border-radius: 0 $border-radius-base 0 $border-radius-base;
}
</style>